﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Diagnostics;
using System.Collections;
using System.Data;
using FirstFloor.ModernUI.Windows;
using System.Threading;


namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarStock.xaml
    /// </summary>
    public partial class ModificarStock : UserControl, IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public ModificarStock()
        {
            InitializeComponent();
        }

        private async void cargarProductos()
        {
            pb.IsIndeterminate = true;

            dgModificarStock.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarProductosTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            pb.IsIndeterminate = false;

            dgModificarStock.ItemsSource = slowTask.Result.DefaultView;

            dgModificarStock.Visibility = System.Windows.Visibility.Visible;

        }

        private DataTable cargarProductosTask()
        {
            return persistencia.obtenerProductos();
        }

        private void dgModificarStock_Row_DoubleClick
                         (object sender, MouseButtonEventArgs e)
        {
            if (this.dgModificarStock.SelectedItem != null)
            {
                Producto p;
                int id_producto;
                String sku;
                String descripcion;
                String fecha_creacion;
                int precio_neto;
                int precio_venta;
                int stock_critico;
                int stock_real;
                int id_categoria;

                try
                {
                    id_producto = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["id_producto"].ToString());
                    sku = ((DataRowView)dgModificarStock.SelectedItem).Row["sku"].ToString();
                    descripcion = ((DataRowView)dgModificarStock.SelectedItem).Row["descripcion"].ToString();
                    fecha_creacion = ((DataRowView)dgModificarStock.SelectedItem).Row["fecha_creacion"].ToString();
                    precio_neto = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["precio_neto"].ToString());
                    precio_venta = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["precio_venta"].ToString());
                    stock_critico = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["stock_critico"].ToString());
                    stock_real = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["stock_real"].ToString());
                    id_categoria = Convert.ToInt32(((DataRowView)dgModificarStock.SelectedItem).Row["id_categoria"].ToString());

                    p = new Producto(id_producto, sku, descripcion, fecha_creacion, precio_neto
                           , precio_venta, stock_critico, stock_real, id_categoria);

                    ModificarStockDialog mpd = new ModificarStockDialog(p);

                    var wnd = new ModernWindow
                    {
                        Style = (Style)App.Current.Resources["EmptyWindow"],
                        Content = mpd,
                        Width = 700,
                        Height = 480,
                        Margin = new Thickness(42),
                        Padding = new Thickness(42),
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        ResizeMode = ResizeMode.NoResize
                    };
                    
                    wnd.ShowDialog();

                    cargarProductos();

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            this.cargarProductos();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
     
        }
    }
}
